<?php

class EventageousAjax extends Eventageous {
	
	private static $messages = array();

	public static function log($logdata, $description=NULL, $tags=array(), $namespace='default', $priority=EL_INFO)
	{
		self::$messages[] = array(
			 'data' 			=> ( is_object($logdata) || is_array($logdata) ? print_r($logdata, true) : $logdata )
			,'description' => ( is_object($description) || is_array($description) ? print_r($description, true) : $description )
			,'tags' 			=> implode(',', $tags)
			,'namespace' 	=> $namespace
			,'priority' 	=> self::$labels[ $priority ]
		);
	}
	
	public static function encapsulate($js) 
	{
		$firebug = array(
			'log' 		=> 'log'
			,'error' 	=> 'error'
			,'warning' 	=> 'warn'
			,'info' 		=> 'info'
			,'debug' 	=> 'debug'
		);
	
		echo '(function () { ';
		
		for ( $i=0; $i < count(self::$messages); $i++ ) 
		{
			$label = strtolower( self::$messages[$i]['priority'] );
			if ( ! isset( $firebug[$label] ) )
				$label = 'log';

			echo 'console.' . $firebug[ $label ] . '(' . json_encode( self::$messages[$i] ) . ');';
		}
		
		echo 'return (' . json_encode($js) . '); ';
		echo '})();';
	}
	
	public static function emergency($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_EMERGENCY);
	}

	public static function alert($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_ALERT);
	}

	public static function critical($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_CRITICAL);
	}

	public static function error($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_ERROR);
	}

	public static function warning($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_WARNING);
	}

	public static function notice($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_NOTICE);
	}

	public static function info($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_INFO);
	}

	public static function debug($logdata, $description=NULL, $tags=array(), $namespace='default')
	{
		self::log($logdata, $description, $tags, $namespace, EL_DEBUG);
	}
	
	public static function log2($arr)
	{
		$vals = array('data', 'description', 'tags', 'namespace', 'priority');
		foreach ( $vals as $v )
			if ( ! isset( $arr[$v] ) )
				$arr[$v] = NULL;
	
		self::log( $arr['data'], $arr['description'], $arr['tags'], $arr['namespace'], $arr['priority'] );
	}
	
	public static function rotate_logs() { }
}

?>